package com.xiaozhen.blog.config;

import com.xiaozhen.blog.controller.interceptor.LoginTicketInterceptor;
import com.xiaozhen.blog.filter.JwtAuthenticationTokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

/**
 * @create 2023-05-11 20:46
 */
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig {

    /**
     * 密码明文加密方式配置
     *
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
     * 获取AuthenticationManager（认证管理器），登录时认证使用
     *
     * @return
     * @throws Exception
     */
    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    }

    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

//    @Autowired
//    private AuthenticationEntryPoint authenticationEntryPoint;
//
//    @Autowired
//    private AccessDeniedHandler accessDeniedHandler;


    /**
     * 配置过滤
     *
     * @param http
     * @return
     * @throws Exception
     */
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
                //前后端分离项目需要关闭 csrf，可惜小圳博客是前后端不分离项目哦
                // 不过后来啊，我回收关闭了 csrf，因为不关闭的话，局部刷新的 url 会报 403
                // 因为局部刷新是通过 ajax 请求的，开启 csrf 的话 Spring Security 不会给认证的
                // 不过有解决办法，只不过小圳还没掌握，就先把 csrf 关闭了吧
                .csrf().disable()
                //STATELESS表示不会通过session获取SecurityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) //
                .and()
                // 添加过滤器到  UsernamePasswordAuthenticationFilter 之前
                .addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class)
                .authorizeRequests() // 过滤请求

                .antMatchers("/**/*.css", "/**/*.js", "/**/*.png", "/**/*.jpg", "/**/*.webp", "/", "/login",
                        "/register", "/kaptcha", "/comments/*", "/blog/*", "/types/*", "/tags/*", "/archives", "/about", "/search"
                        , "/like","/sendMsg") //
                .permitAll() // 都可以访问

                // .antMatchers("/login", "/", "/kaptcha").anonymous() //未登录可以访问，登录不可以访问
                //除上面外的所有请求全部需要认证即可访问
                .anyRequest().authenticated();

        // 添加异常处理器
//        http
//                .exceptionHandling()
//                .authenticationEntryPoint(authenticationEntryPoint)
//                .accessDeniedHandler(accessDeniedHandler);

        return http.build();
    }
}

